package com.start.shop.modules.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.start.shop.modules.repository.dao.SkuDao;
import com.start.shop.modules.repository.entity.Sku;
import com.start.shop.modules.repository.entity.SkuAttr;
import com.start.shop.modules.service.ISkuService;


@Service
public class SkuServiceImpl implements ISkuService {
	
	private Logger logger = LoggerFactory.getLogger(ISkuService.class);
	
	@Autowired
	SkuDao skuDao;
	
	
	/*
	 * 查询所有商品类型
	 */
	@Override
	//@Cacheable(cacheNames = "Shop::getAllSkuByGoodsId", key = "'goodsId' + '-' +#goodsId")
	public List<Sku> getAllSkuByGoodsId(int goodsId) {
		return skuDao.selectAllSkuByGoodsId(goodsId);
	}

	/*
	 * 查询所有商品类型
	 */
	//@Cacheable(cacheNames = "Shop::getSkuBySkuId", key = "'skuId' + '-' +#skuId")
	@Override
	public Sku getSkuBySkuId(int skuId) {
		return skuDao.selectSkuBySkuId(skuId);
	}

	/*
	 * 商品出库
	 */
	@Override
	public boolean stockOut(int skuId, int num) {
		Sku sku = skuDao.selectSkuBySkuId(skuId);
		if(sku == null) { //商品不存在
			logger.error("【该商品不存在】skuId:{} ",skuId);
			return false;
		}
		if(sku.getStock() < num) {
			logger.error("【该商品库存不足】sotck:{} num:{} ",sku.getStock(), num);
			return false;
		}
		skuDao.skuStockOut(skuId, num);
		
		return true;
	}
	
	/*
	 * 商品入库
	 */
	@Override
	public boolean stockIn(int skuId, int num) {
		if(skuDao.skuStockIn(skuId, num)>0) {
			return true;
		}
		return false;
	}

	@Override
	//@Cacheable(cacheNames = "Shop::getSkuAttrById", key = "'skuId' + '-' +#skuId")
	public SkuAttr getSkuAttrById(int id) {
		return skuDao.selectSkuAttrById(id);
	}
	
	
	@Override
	//@Cacheable(cacheNames = "Shop::getSkuAttrById", key = "'skuId' + '-' +#skuId")
	public List<SkuAttr> getSkuAttrByPid(int pid) {
		return skuDao.selectSkuAttrByPid(pid);
	}
}
